package controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import dto.AuctionDTO;
import dto.BoardDTO;
import dto.ListAuctionDTO;
import dto.ListBoardDTO;
import dto.MemberDTO;
import dto.PagingDTO;
import dto.ProductDTO;

import service.AuctionManagerService;
import util.Utilities;

@Controller
public class AuctionController {
	private AuctionManagerService service;
	private String uploadDirectory;

	public AuctionController(AuctionManagerService service) {
		this.service = service;
	}
	
	@RequestMapping("registerAuctionForm.do")
	public ModelAndView registerAuctionForm(HttpSession session){
		ModelAndView modelAndView = null;
		if(session.getAttribute("memberDTO")!=null){
			
			ArrayList<String> category = service.getCategory();
			
			ArrayList<String> location = (ArrayList<String>) service.getLocationList();
			
			HashMap<String, List> map = new HashMap<String, List>();
			
			map.put("category", category);
			map.put("location", location);
			
			modelAndView = new ModelAndView("tilesAuctionRegisterForm.do", map);
		} else {
			modelAndView = new ModelAndView("loginTiles.do", "error_message", "로그인 먼저 해주세요");
		}
		
		
		return modelAndView;
	}
	
	public AuctionController(AuctionManagerService service,
			String uploadDirectory) {
		super();
		this.service = service;
		this.uploadDirectory = uploadDirectory;
	}

	@RequestMapping("registerAuction.do")
	public ModelAndView registerAuction(AuctionDTO auctionDTO, HttpSession session){
		ModelAndView modelAndView = null;
		String fileName = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		if(member!=null){			
			if(auctionDTO.getUpfile()!= null && !auctionDTO.getUpfile().isEmpty()){
				fileName = ""+System.currentTimeMillis()+auctionDTO.getUpfile().getOriginalFilename().substring(auctionDTO.getUpfile().getOriginalFilename().lastIndexOf("."));
				auctionDTO.setFile(fileName);
				try {
					auctionDTO.getUpfile().transferTo(new File(uploadDirectory, fileName));
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				service.registerAuction(auctionDTO, member);
				
				modelAndView = new ModelAndView("tilesAuctionDetail.do", "auction", auctionDTO);
				
			} else {
				modelAndView = new ModelAndView("loginTiles.do", "error_message", "로그인 먼저 해주세요");
			}
		}
		
		return modelAndView;
	}
	
//	@RequestMapping("auctionList.do")
//	public ModelAndView auctionList(){
//		ModelAndView modelAndView = null;
//		
//		modelAndView = new ModelAndView("tilesAuctionList.do", "auctionList", service.getAuctionList());
//		
//		return modelAndView;
//	}
	
	@RequestMapping("myAuctionList.do")
	public ModelAndView myAuctionList(HttpSession session, int page){
		ModelAndView modelAndView = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		
		if(member != null){
			ListAuctionDTO list = service.getMyAuctionList(member.getId(), page);
			
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			map.put("list", list);
			map.put("page", page);
			
			modelAndView = new ModelAndView("tilesMyAuctionList.do", map );
		}
		
		return modelAndView;
	}
	@RequestMapping("auctionList.do")
	public ModelAndView auctionList(@RequestParam(defaultValue="1") int page){
		ModelAndView modelAndView = null;
		ListAuctionDTO list = service.getAuctionListByPage(page);
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", list);
		map.put("page", page);
		
		modelAndView = new ModelAndView("tilesAuctionListPaging.do", map);
		
		return modelAndView;
	}
	
	@RequestMapping("auctionDetail.do")
	public ModelAndView auctionDetail(Integer auctionNo){
		ModelAndView modelAndView = null;
		
		AuctionDTO auctionDTO = service.getAuctionByAuctionNo(auctionNo);
		
		modelAndView = new ModelAndView("tilesAuctionDetail.do", "auction", auctionDTO);
		
		return modelAndView;
	}
	
	@RequestMapping("auctionCategoryList.do")
	public ModelAndView auctionCategoryList(){
		ModelAndView modelAndView = null;
		
		ArrayList<String> category = service.getCategory();
		
		modelAndView = new ModelAndView("jsonView", "category" , category);
		
		return modelAndView;
	}
	
	@RequestMapping("auctionSearch.do")
	public ModelAndView auctionSearch(String auctionName, String category, @RequestParam(defaultValue="1")int page){
		ModelAndView modelAndView = null;
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		map.put("page", page);
		
		if(auctionName!=null){
			ListAuctionDTO list = service.getAuctionListByAuctionName(auctionName, page);
			map.put("list", list);
			modelAndView = new ModelAndView("tilesAuctionSearch.do", map);
		} else if(category != null){
			ListAuctionDTO list = service.getAuctionListByCategory(category, page);
			map.put("list", list);
			modelAndView = new ModelAndView("tilesAuctionSearch.do", map);
		}
		
		return modelAndView;
	}
	
	
	@RequestMapping("modifyAuctionForm.do")
	public ModelAndView modifyAuctionForm(HttpSession session,int auctionNo){
		ModelAndView modelAndView = null;
		
		if(session.getAttribute("memberDTO")!=null){
			
			ArrayList<String> category = service.getCategory();
			
			ArrayList<String> location = (ArrayList<String>) service.getLocationList();
			
			AuctionDTO auction = service.getAuctionByAuctionNo(auctionNo);
			
			auction.setAuctionContent(Utilities.changeContentForTextArea(auction.getAuctionContent()));
			
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			map.put("category", category);
			map.put("location", location);
			map.put("auction", auction);
			
			modelAndView = new ModelAndView("tilesAuctionModifyForm.do", map);
		} else {
			modelAndView = new ModelAndView("loginTiles.do", "error_message", "로그인 먼저 해주세요");
		}
		
		return modelAndView;
	}
	
	@RequestMapping("modifyAuction.do")
	public ModelAndView modifyAuction(HttpSession session,int auctionNo, AuctionDTO auctionDTO){
		ModelAndView modelAndView = null;
		String fileName = null;
		MemberDTO member = (MemberDTO) session.getAttribute("memberDTO");
		AuctionDTO auction = service.getAuctionByAuctionNo(auctionNo);
		
		auctionDTO.setRegisterDate(Utilities.changeDateFormatForDB(auction.getRegisterDate()));
		
		if(member!=null){			
			if(auctionDTO.getUpfile()!= null && !auctionDTO.getUpfile().isEmpty()){
				fileName = ""+System.currentTimeMillis()+auctionDTO.getUpfile().getOriginalFilename().substring(auctionDTO.getUpfile().getOriginalFilename().lastIndexOf("."));
				auctionDTO.setFile(fileName);
				try {
					File file = new File(uploadDirectory, auction.getFile());
					if(file.exists()){						
						file.delete();
					}
					auctionDTO.getUpfile().transferTo(new File(uploadDirectory, fileName));
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				service.updateAuction(auctionDTO, member);
				session.setAttribute("memberDTO", auctionDTO);
				
				modelAndView = new ModelAndView("tilesAuctionDetail.do", "auction", auctionDTO);
				
			} else{
				service.updateAuction(auctionDTO, member);
				modelAndView = new ModelAndView("tilesAuctionDetail.do", "auction", auctionDTO);
			}
		}else {
			modelAndView = new ModelAndView("loginTiles.do", "error_message", "로그인 먼저 해주세요");
		}
		
		return modelAndView;
	}
	
	@RequestMapping("deleteAuction.do")
	public ModelAndView deleteAuction(int auctionNo, HttpSession session){
		ModelAndView modelAndView = null;
		
		if(session.getAttribute("memberDTO") != null){
			AuctionDTO auction = service.getAuctionByAuctionNo(auctionNo);
			if(auction.getFile() != null){
				try {
					File file = new File(uploadDirectory, auction.getFile());
					if(file.exists()){						
						file.delete();
					}
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			service.removeAuction(auctionNo);
			
			modelAndView = new ModelAndView("auctionList.do", "page", 1);
		} else{
			modelAndView = new ModelAndView("loginTiles.do", "error_message", "로그인 먼저 해주세요");
		}
		
		return modelAndView;
	}
}
